const request=require('../../util/request.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        viewList:[],
        mindate:Math.round((new Date() - 365*86400*1000)),
        // index:  1,//NOTE 这里的index会影响到页面遍历时的临时变量index ，重名了，wx:for时默认的临时索引变量名就叫index
        showstarttime:false,
        showlasttime:false,
        order_time:"",
        delivery_time:"",
        __returnParams: null,
        activeId: null, // 当前选中的 cell id
        nameToid:[]//负责人页面显示名字,提交时要提交id
    },
    refreshCalendar(type) {
      this.setData({
        [`timestamp${type}`]: new Date().getTime()
      })
    },
    onDisplay(event) {
      this.data.activeId=event.currentTarget.dataset.id
      this.setData({ showstarttime: true });
    },
    onDisplays(event) {
      this.data.activeId=event.currentTarget.dataset.id
      this.setData({ showlasttime: true });
    },
    onClose() {
      this.setData({ showstarttime: false });
      this.setData({ showlasttime: false });
    },
    start(event) {
        const tmptime = this.formatDate(event.detail)
        let tmpNum = this.data.activeId
        var arrName = event.currentTarget.dataset.arrName;
        var variable = event.currentTarget.dataset.variable;
        var arr = this.data[arrName];
        arr[tmpNum][variable] = tmptime
        this.setData({
            [arrName]:arr
        })
        this.onClose()
    },

    last(event) {
      const tmptime = this.formatDate(event.detail)
      let tmpNum = this.data.activeId
      var arrName = event.currentTarget.dataset.arrName;
      var variable = event.currentTarget.dataset.variable;
      var arr = this.data[arrName];
      arr[tmpNum][variable] = tmptime
      this.setData({
          [arrName]:arr
      })
      this.onClose()
    },

    formatDate(date) {
      date = new Date(date);
      let tmp = `${date.getMonth()+1}`
      if(tmp<10){
        tmp = 0+tmp
      }
      return `${date.getFullYear()}-`+tmp+`-${date.getDate()}`;
    },
    formatDates(date) {
      date = new Date(date);
      return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`;
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
    },
    addNewView() {
        // 获取当前viewList
        let currentList = this.data.viewList
        let currentList1 = this.data.nameToid
        currentList.push({ order_time:"", order_time:"",order_no:"",customer_name:"",p_no:"",p_name:"",sort:"",pic_name:"",pic_no:"",unit:"",num:"",delivery_time:"",delivery_address:"",manager:""})
        currentList1.push({manager:""})
        this.setData({
            viewList: currentList,
            nameToid: currentList1
        })
    },
    submit1(event) {
      const callback = () =>{
        let currentList = this.data.viewList
        currentList.splice(0,currentList.length)
        this.setData({
            viewList: currentList,
        })
      }
    
      for(let i = 0;i<this.data.viewList.length;i++){
        this.data.viewList[i].manager = this.data.nameToid[i].manager
      }
      let formData = this.data.viewList
      let tmp = {"orders":formData}
      console.log(tmp,"tmp")
      request.requestAjax("order/batch_add_order",tmp,"上传中",function(res){
          if (res.status === 200) {
            wx.showToast({
            title: '提交成功',
            icon: 'success',
            success:callback
          });
          
        }
      },
        function (res) {
          wx.showToast({
            title: '错误',
            icon:'none'
          })
      })
    },
    bindKeyInputVant(event) {0
        let valueName = event.target.dataset.variable;
        this.setData({
            [valueName]: event.detail
        });
    },
    //用于 数组 对象 具体属性值的同步修改 
    bindKeyInputVant1(event) {
        var arrName = event.currentTarget.dataset.arrName;
        var index = event.currentTarget.dataset.index;
        var variable = event.currentTarget.dataset.variable;
        var arr = this.data.viewList;
        arr[index][variable] = event.detail
        this.setData({
            [arrName]:arr
        })
        //NOTE 最终查看写入结果
        // console.log(this.data[arrName])
    },
    delData(data){
      let currentList = this.data.viewList
      //NOTE 获取传来的index
      var index = data.currentTarget.dataset.index;
      currentList.splice(index, 1)
        this.setData({
            viewList: currentList
        })
    },

    delRow(event){
      var that = this;
      wx.showModal({
        title: '提示',
        content: '确定删除当前数据吗？',
        success: function (res) {
          if (res.confirm) {
            that.delData(event)
          } 
        }
      })
    },
    selmanager(event){
      var index = event.currentTarget.dataset.index
      wx.navigateTo({
        url: `/pages/selmanager/selmanager?index=${index}`,
      })
    },
    onShow() {
      if (this.data.__returnParams) {
        if(this.data.__returnParams[0] === "0"){
          this.data.__returnParams[1] = "暂不选择"
        }
        let valueNum = this.data.viewList[this.data.__returnParams[2]]
        let valueid = this.data.nameToid[this.data.__returnParams[2]]
        let valueName = this.data.viewList
        let valueName1 = this.data.nameToid
        valueNum.manager = this.data.__returnParams[1],
        valueid.manager = Number(this.data.__returnParams[0]) 
        this.setData({ 
          viewList:valueName,
          nameToid:valueName1,
          __returnParams: null 
        }) // 清空避免重复触发
      }
    },
    setManager(){
      if (this.data.__returnParams) {
        if(this.data.__returnParams[0] === "0"){
          this.data.__returnParams[1] = "暂不选择"
        }
        let valueNum = this.data.viewList[this.data.__returnParams[2]]
        let valueid = this.data.nameToid[this.data.__returnParams[2]]
        let valueName = this.data.viewList
        let valueName1 = this.data.nameToid
        valueNum.manager = this.data.__returnParams[1],
        valueid.manager = Number(this.data.__returnParams[0]) 
        this.setData({ 
          viewList:valueName,
          nameToid:valueName1,
          __returnParams: null 
        }) // 清空避免重复触发
      }
    },
    
})